package com.controller;


import com.Service.MenuService;
import com.Service.UserMenu;
import com.ServiceImpl.MenuServiceImpl;
import com.dao.CarDao;
import com.pojo.Car;
import com.pojo.Record;
import com.pojo.User;

import java.util.List;
import java.util.Scanner;

public class UserMenuImpl implements UserMenu {
    private Scanner sc;
    private User user;
    private MenuService menuService;

    public UserMenuImpl(User user) {
        sc = new Scanner(System.in);
        this.user = user;
        menuService = new MenuServiceImpl();
    }

    @Override
    public void main_Menu() {
        List<Car> allCar = menuService.getAllCar(user);
        System.out.println("编号\t汽车名称\t备注\t品牌\t类型\t价格\t是否可租");
        if (allCar.size()==0){
            System.out.println("没有车辆信息");
        }else{
            allCar.forEach(car -> System.out.println(car));
        }
        System.out.println("输入0退出");
        System.out.println("输入1查看所有汽车");
        System.out.println("输入2+1升序查看汽车 输入2+2降序查询车辆");
        System.out.println("输入3+类型 按照类型查询车辆");
        System.out.println("输入4+品牌 按照品牌查询车辆");
        System.out.println("输入5查看本人租赁信息");
        System.out.println("输入6+汽车编号 租赁指定车辆");
        System.out.println("输入7归还车辆");
        String choice = sc.next();
        if (choice.length() > 2) {
            String[] str = choice.split("\\+");
            switch (str[0]) {
                case "2":
                    showCar_Sort(str[1]);
                    break;
                case "3":
                    showCarByType(str[1]);
                    break;
                case "4":
                    showCarByBrand(str[1]);
                    break;
                case "6":
                    rentCarById(str[1]);
                    break;

                default:
                    System.out.println("输入有误");
                    main_Menu();
                    return;
            }

        } else {
            switch (choice) {
                case "0":
                    new CommonMenuImpl().choice();
                    return;
                case "1":
                    showAllCar();
                    break;
                case "5":
                    showRecord(user);
                    break;
                case "7":
                    backCarById(user);
                    break;
                default:
                    System.out.println("输入错误");
                    main_Menu();
                    return;
            }

        }

    }

    @Override
    public void showAllCar() {
        List<Car> allCar = menuService.getAllCar(user);
        if (allCar == null) {
            System.out.println("没有车辆信息");
        } else {
            System.out.println("编号\t汽车名称\t备注\t品牌\t类型\t价格\t是否可租");
            allCar.forEach(car -> System.out.println(car));
        }
        back();
    }

    @Override
    public void showCar_Sort(String choice) {

        if (choice.equals("1") || choice.equals("2")) {
            List<Car> sortCar = menuService.getSortCar(choice, user);
            showCars(sortCar);
        } else {
            System.out.println("输入错误");
            back();
        }

        back();
    }

    @Override
    public void showCarByType(String type) {
        List<Car> cars = menuService.getCarByType(type, user);
        showCars(cars);
    }

    @Override
    public void showCarByBrand(String brand) {
        List<Car> cars = menuService.getCarByBrand(brand, user);
        showCars(cars);
    }

    public void showCars(List<Car> cars) {
        if (cars.size()==0) {
            System.out.println("没有车辆信息");
            back();
        } else {
            System.out.println("编号\t汽车名称\t备注\t品牌\t类型\t价格\t是否可租");
            cars.forEach(car -> System.out.println(car));
        }
        back();
    }

    @Override
    public void showRecord(User user) {
        List<Record> records=menuService.getRecordsByUser(user);
        if (records.size()==0) {
            System.out.println("没有借车");
        } else {
            System.out.println("订单编号\t用户编号\t车辆编号\t借车时间\t还车时间\t金额");
            records.stream().forEach(record -> System.out.println(record));
        }
        back();
    }

    @Override
    public void rentCarById(String id) {
        Record record = menuService.rentCarById(id, user);
        if (record==null) {
            System.out.println("租车失败");
        } else {
            System.out.println("订单编号\t用户编号\t车辆编号\t借车时间\t还车时间\t金额");
            System.out.println(record);
        }
        back();
    }

    @Override
    public void backCarById(User user) {
        List<Record> allRecord = menuService.getRecordsByUser(user);

        if (allRecord.size() == 0) {
            System.out.println("没有借车");
        } else {
            System.out.println("订单编号\t用户编号\t车辆编号\t借车时间\t还车时间\t金额");
            allRecord.stream().forEach(record -> System.out.println(record));
            System.out.println("请输入订单编号");
            String id = sc.next();
            Record recordById = menuService.getRecordByid(id);
            if (recordById==null){
                System.out.println("没有找到订单");
                back();
            }
            Car carById = CarDao.getCarById(recordById.getCar_id());
            if (recordById.getUser_id().equals(user.getId()) && recordById.getBackTime() == null) {
                Record record = menuService.backCar(id,carById.getPrice());
                System.out.println("本次花费" + record.getPrice() + "元");
            } else {
                System.out.println("订单号输入错误");
            }
        }
        back();
    }

    public void back() {
        System.out.println("输入任意字符返回上一级");
        sc.next();
        main_Menu();
    }
}
